//
//  GRTableViewController.m
//  GRFMDownloader_Example
//
//  Created by Guo Rui on 2018/1/13.
//  Copyright © 2018年 GR. All rights reserved.
//

#import "GRTableViewController.h"
#import "GRDownloadCell.h"

@interface GRTableViewController ()
@property (strong, nonatomic) NSMutableArray *datas;
@end

@implementation GRTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 65;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"GRDownloadCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ID"];
    self.datas = [NSMutableArray arrayWithCapacity:0];
    
    // http://120.25.226.186:32812/resources/videos/minion_01.mp4
    NSString *baseURL = @"http://120.25.226.186:32812/resources/videos/";
    for (int i = 1; i <= 6; i++) {
        NSString *url = [baseURL stringByAppendingFormat:@"minion_%02zd.mp4", i];
        NSString *name = [NSString stringWithFormat:@"小黄人 第%02zd部", i];
        NSDictionary *dict = @{ @"url" : url, @"name" : name };
        [self.datas addObject:dict];
    }
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.datas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID" forIndexPath:indexPath];
    
    NSDictionary *dict = [self.datas objectAtIndex:indexPath.row];
    [cell updateWithFileName:dict[@"name"]];
    [cell downloadWithURL:[NSURL URLWithString:dict[@"url"]]];
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
